<?php
class JobMysql extends Job implements IJob
{
  protected $databases = null;
  
  public function doBackup()
  {
    $h = "";
    if($this->host!=null)
    {
      $h = " -h " . $this->host;
    }
    $u = "";
    if($this->user!=null)
    {
      $u = " -u " . $this->user;
    }
    $p = "";
    if($this->password!=null)
    {
      $p = " --password=" . $this->password;
    }
    $d = "";
    if($this->databases!=null)
    {
      $d = " --databases " . $this->databases;
    }
    
    if(!is_dir(dirname($this->getDestinationBackup())))
    {
      mkdir(dirname($this->getDestinationBackup()), 0660, true);
    }
        
    $cmds   = array();
    $cmds[] = $this->pathMysqlBin . "mysqldump --add-locks --create-options --quick --extended-insert --set-charset --triggers --compress" . $h . $u . $p . $d . " > " . $this->getDestinationBackup();
    
    $this->execCommandos($cmds);
  }
  
  public function getFileExtensionBackup()
  {
    return ".sql";
  }
  
  public function getFileExtensionCompression()
  {
    return "";
  }
  
  public function getFileIdentifier()
  {
    return $this->databases;
  }
  
  public function postLoadJob()
  {
    return;
  }
  
  public function setDatabases($databases)
  {
    $this->databases = $databases;
  }
  
  public function setOptions($optionsElement)
  {
    foreach($optionsElement->children() as $optionElement)
    {
      switch($optionElement->getName())
      {
        case "Databases":
          $this->setDatabases((string)$optionElement);
          break;
                        
        default:
          $this->logger->warn("Unknown option element '" . $optionElement->getName() . "'.");
          break;
      }
    }    
  }
}